import {
    Action
} from 'redux';

import * as types from './../types/user';
import * as commonTypes from './../types/common';

interface _Action extends Action {
    data:any
}

const initState:any = {
    loginUser: {},
    profiles: {},
    matches: {},
    userStatus:{}
};

export default (
    state = initState,
    action: _Action
) => {

    const {
        type,
        data
    } = action;

    switch (type) {
        case types.deleteUserPhoto : {
            const {
                loginUser
            } = state;

            const {
                photos = []
            } = loginUser as any;

            const index = photos.findIndex((item: any) => (
                item.photoId === data
            ))

            if (index > -1) {
                photos.splice(index, 1);
                return {
                    ...state,
                    loginUser: {
                        ...loginUser,
                        photos: [...photos]
                    }
                }
            }
        }

        case types.userLogin : {
            const {
                loginUser
            } = state;

            state.loginUser = {
                ...loginUser,
                ...data
            }
            return {
                ...state
            }
        }

        case types.userLoginOut : {
            state.loginUser = {};
            return {
                ...state
            }
        }

        case types.userDelete: {
            state.loginUser = {};
            return {
                ...state
            }
        }

        case types.getUserProfile : {
            const {
                profiles = {}
            } = state;

            const {
                userId
            } = data;

            profiles[userId] = data;
            return {
                ...state,
                profiles: profiles
            }
        }

        case types.getUserMatch : {
            const {
                userId
            } = data;

            const {
                matches = {}
            } = state

            matches[userId] = data;

            return {
                ...state,
                matches: matches
            }
        }

        case commonTypes.postLikeUser: {
            const {
                userId
            } = data;

            const {
                profiles
            } = state;

            const _item: any = (
                profiles[userId]
            )

            if (!!_item) {
                _item.likeStatus = 1;
                _item.likeTotal += 1;

                profiles[userId] = {..._item};

                return {
                    ...state,
                    profiles: profiles
                }
            }

            return state;
        }

        case commonTypes.resetLikeUser : {
            const {
                userId
            } = data;

            const {
                profiles
            } = state;

            const _item: any = (
                profiles[userId]
            )

            if (!!_item) {
                _item.likeStatus = 0;
                _item.likeTotal += -1;

                profiles[userId] = {..._item};

                return {
                    ...state,
                    profiles: profiles
                }
            }

            return state;
        }

        case commonTypes.postFollowUser : {
            const {
                userId
            } = data;

            const {
                profiles
            } = state;

            const _item: any = (
                profiles[userId]
            )

            if (!!_item) {
                _item.isFollowing = true;
                _item.followTotal += 1;

                profiles[userId] = {..._item};

                return {
                    ...state,
                    profiles: profiles
                }
            }

            return state;
        }

        case commonTypes.resetFollowUser : {
            const {
                userId
            } = data;

            const {
                profiles
            } = state;

            const _item: any = (
                profiles[userId]
            )

            if (!!_item) {
                _item.isFollowing = false;
                _item.followTotal += -1;

                profiles[userId] = {..._item};

                return {
                    ...state,
                    profiles: profiles
                }
            }

            return state;
        }

        case types.getUserCoin: {
            const {
                loginUser
            } = state as any;

            const {
                amount = 0
            } = data;
            loginUser.coinAmount = amount;
            return {
                ...state,
                loginUser: loginUser
            }
        }

        case types.getUserItemsTotal:{
            state.userStatus = {...data};
        }
    }

    return state;
}